#include <bits/stdc++.h>
using namespace std;

const int N=2005;
int T,n,a[N],c[25],pos[N][N],cnt[N];
long long ans;
struct Good{
    int l,r,v;
} t[N];
vector <Good> g;

void dfs(int x){
    if (x>n){
        int r=0,b=0;
        long long cnt=0;
        for (int i=1;i<=n;i++){
            if (c[i]==1){
                if (a[r]==a[i]) cnt+=a[i];
                r=i;
            } else {
                if (a[b]==a[i]) cnt+=a[i];
                b=i;
            }
        }
        ans=max(ans,cnt);
        return;
    }
    c[x]=1;
    dfs(x+1);
    c[x]=2;
    dfs(x+1);
    return;
}

int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin >> T;
    while (T--){
        scanf("%d", &n);
        ans=0;
        memset(pos,0,sizeof pos);
        memset(cnt,0,sizeof cnt);
        if (n<=20){
            for (int i=1;i<=n;i++) scanf("%d", &a[i]);
            dfs(1);
            printf("%lld\n", ans);
        } else {
            for (int i=1;i<=n;i++){
                scanf("%d", &a[i]);
                pos[a[i]][++cnt[a[i]]]=i;
            }
        }
    }
    return 0;
}
